<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="預約編碼" prop="reservationCode">
        <el-input
          v-model="queryParams.reservationCode"
          placeholder="請輸入預約編碼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客戶編碼" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="請輸入客戶編碼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客戶姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="請輸入客戶姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="聯繫方式" prop="telphone">
        <el-input
          v-model="queryParams.telphone"
          placeholder="請輸入聯繫方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="電子郵箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="請輸入電子郵箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="客戶地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="請輸入客戶地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="邀約人ID" prop="inviterId">
        <el-input
          v-model="queryParams.inviterId"
          placeholder="請輸入邀約人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="邀約人" prop="inviterName">
        <el-input
          v-model="queryParams.inviterName"
          placeholder="請輸入邀約人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="預約事項" prop="matter">
        <el-select
          v-model="queryParams.matter"
          placeholder="請選擇預約事項"
          clearable
        >
          <el-option
            v-for="dict in dict.type.reservation_matter"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="預約時間" prop="reservationTime">
        <el-date-picker
          clearable
          v-model="queryParams.reservationTime"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="請選擇預約時間"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="接待人ID" prop="receiverId">
        <el-input
          v-model="queryParams.receiverId"
          placeholder="請輸入接待人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="接待人姓名" prop="receiverName">
        <el-input
          v-model="queryParams.receiverName"
          placeholder="請輸入接待人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="完成時間" prop="completeTime">
        <el-date-picker
          clearable
          v-model="queryParams.completeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="請選擇完成時間"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="報價ID" prop="quoterId">
        <el-input
          v-model="queryParams.quoterId"
          placeholder="請輸入報價ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="報價編碼" prop="quoterCode">
        <el-input
          v-model="queryParams.quoterCode"
          placeholder="請輸入報價編碼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="發起時間" prop="applyTime">
        <el-date-picker
          clearable
          v-model="queryParams.applyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="請選擇發起時間"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['biz:reservation:query']"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:reservation:add']"
          >新增</el-button
        >
      </el-col>
      <!-- managing_director -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:reservation:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:reservation:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:reservation:export']"
          >導出</el-button
        > -->
      </el-col>
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="reservationList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        show-overflow-tooltip
        width="180"
        label="預約編碼"
        align="center"
        prop="reservationCode"
      />
      <el-table-column
        width="180"
        label="客戶編碼"
        align="center"
        prop="customerCode"
      />
      <el-table-column
        width="120"
        label="客戶姓名"
        align="center"
        prop="customerName"
      />
      <el-table-column
        show-overflow-tooltip
        label="聯繫方式"
        width="120"
        align="center"
        prop="telphone"
      />
      <el-table-column
        width="180"
        show-overflow-tooltip
        label="電子郵箱"
        align="center"
        prop="email"
      />
      <el-table-column
        show-overflow-tooltip
        width="120"
        label="客戶地址"
        align="center"
        prop="address"
      />
      <el-table-column
        show-overflow-tooltip
        label="預約方式"
        align="center"
        prop="reservationType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.reservation_type"
            :value="scope.row.reservationType"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="邀約人"
        align="center"
        prop="inviterName"
      />
      <el-table-column
        show-overflow-tooltip
        label="備註"
        align="center"
        prop="content"
      />
      <el-table-column
        label="預約時間"
        align="center"
        prop="reservationTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.reservationTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="接待人"
        align="center"
        prop="receiverName"
      />
      <el-table-column
        show-overflow-tooltip
        label="完成時間"
        align="center"
        prop="completeTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.completeTime }}</span>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="預約狀態"
        align="center"
        prop="reservationStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.reservation_status"
            :value="scope.row.reservationStatus"
          />
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="報價狀態"
        align="center"
        prop="quoterStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.quote_status"
            :value="scope.row.quoterStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="報價編碼"
        align="center"
        prop="quoterCode"
      />
      <el-table-column
        show-overflow-tooltip
        label="發起邀約時間"
        align="center"
        prop="applyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="200"
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.reservationStatus != '10'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:reservation:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.reservationStatus != '10'"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:reservation:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >詳情</el-button
          >
          <div>
            <el-button
              style="padding: 5px"
              size="mini"
              :disabled="scope.row.reservationStatus != '50'"
              type="primary"
              v-hasPermi="['biz:reservation:initiate']"
              @click="GoToQuote(scope.row)"
              >發起報價</el-button
            >
            <!-- :disabled="scope.row.completeTime != null||scope.row.reservationStatus == '10'" -->
            <el-button
              style="padding: 5px"
              :disabled="
                scope.row.reservationStatus == '10' ||
                scope.row.reservationStatus == '20'
              "
              @click="completeBtn(scope.row)"
              type="primary"
              v-hasPermi="['biz:reservation:complete']"
              size="mini"
              >完成</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 完成彈窗 -->
    <el-dialog
      :visible.sync="open_receiverName"
      destroy-on-close
      :close-on-click-modal="false"
      width="400px"
      append-to-body
    >
      <el-form
        ref="form_complete"
        :model="form_complete"
        :rules="rules_complete"
        label-width="80px"
      >
        <el-form-item label="接待人" prop="receiverName">
          <el-input
            disabled
            v-model="form_complete.receiverName"
            placeholder="請選擇接待人姓名"
          >
            <el-button
              slot="append"
              @click="open_dialog_receiverName"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="完成時間" prop="completeTime">
          <el-date-picker
            style="width: 100%"
            clearable
            v-model="form_complete.completeTime"
            type="datetime"
            @input="$forceUpdate()"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="請選擇完成時間"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div style="text-align: center; margin-top: 16px">
        <el-button type="primary" @click="submitForm_complete">提 交</el-button>
      </div>

      <!-- 選擇接待人  -->
      <el-dialog
        :visible.sync="dialog_if_receiver"
        destroy-on-close
        :close-on-click-modal="false"
        width="800px"
        append-to-body
      >
        <receiver-table
          :selected="selected_receiver_id"
          @out_detail="receiver_detail"
          @out_cancel="receiver_cancel"
        >
        </receiver-table>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReservation,
  getReservation,
  delReservation,
  addReservation,
  putReservationComplete,
  updateReservation,
} from "@/api/biz/reservation";
import receiverTable from "@/components/biz/receiverTable";
import { listUser } from "@/api/system/user";

export default {
  name: "Reservation",
  components: {
    receiverTable,
  },
  dicts: [
    "reservation_matter",
    "reservation_status",
    "reservation_type",
    "quote_status",
    "sys_user_sex",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中數組
      ids: [],
      // 子表选中數据
      checkedBizCustomerReservationItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 顯示搜索条件
      showSearch: true,
      // 总条數
      total: 0,
      // 客戶预约表格數据
      reservationList: [],
      // 預約事項明細表格數据
      bizCustomerReservationItemList: [],
      // 弹出层標题
      title: "",
      // 是否顯示弹出层
      isAsc: "desc",
      // 查询參數
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        customerCode: null,
        customerName: null,
        telphone: null,
        email: null,
        address: null,
        reservationType: null,
        inviterId: null,
        inviterName: null,
        matter: null,
        content: null,
        reservationTime: null,
        receiverId: null,
        receiverName: null,
        completeTime: null,
        reservationStatus: null,
        quoterStatus: null,
        quoterId: null,
        quoterCode: null,
        applyTime: null,
        delFag: null,
        orderByColumn: "update_time",
        isAsc: "desc",
      },
      // 表单參數
      form: {},
      form_complete: {},
      // 表单校验
      rules: {
        email: [
          { required: true, message: "電子郵箱不能為空", trigger: "blur" },
        ],
      },
      rules_complete: {
        // completeTime: [
        //   { required: true, message: "請選擇完成時間", trigger: "blur" },
        // ],
        // receiverName: [
        //   { required: true, message: "請選擇接待人", trigger: "blur" },
        // ],
      },

      open_receiverName: false,
      dialog_if_receiverName: false,
      multipleSelection_receiverName: [],
      select_customer_receiverName: {},
      userList_receiverName: [],

      // 選擇接待人詳細信息
      dialog_if_receiver: false,
      selected_receiver_id: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 選擇接待人詳細信息----------------------------------
    open_dialog_receiverName() {
      this.dialog_if_receiver = true;
    },
    receiver_detail(item) {
      this.selected_receiver_id = item.userId;

      this.form_complete.receiverName = item.receiverName;
      this.form_complete.receiverId = item.userId;
      this.dialog_if_receiver = false;
    },
    receiver_cancel() {
      this.dialog_if_receiver = false;
    },
    // ----------------------------------------------------------

    /** 查询客戶预约列表 */
    getList() {
      this.loading = true;
      listReservation(this.queryParams)
        .then((response) => {
          this.reservationList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        customerCode: null,
        customerName: null,
        telphone: null,
        email: null,
        address: null,
        reservationType: null,
        inviterId: null,
        inviterName: null,
        matter: null,
        content: null,
        reservationTime: null,
        receiverId: null,
        receiverName: null,
        completeTime: null,
        reservationStatus: null,
        quoterStatus: null,
        quoterId: null,
        quoterCode: null,
        applyTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFag: null,
      };
      this.bizCustomerReservationItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中數据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single =
        selection.length !== 1 || selection[0].reservationStatus != "10";
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      console.log(1111111111);
      console.log(this.$router);
      this.$router.push({ path: "/reservation/index/add" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const id = row.id || this.ids;
      this.$router.push({ path: "/reservation/index/edit", query: { id: id } });
      // getReservation(id).then((response) => {
      //   this.form = response.data;
      //   this.$router.push({
      //     path: "/reservation/index/edit",
      //     query: response.data,
      //   });
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.bizCustomerReservationItemList =
            this.bizCustomerReservationItemList;
          if (this.form.id != null) {
            updateReservation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReservation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否確認删除客戶预约編號為"' + ids + '"的數据项？')
        .then(function () {
          return delReservation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleDetail(row) {
      const id = row.id || this.ids;
      this.$router.push({
        path: "/reservation/index/detail",
        query: { id: id },
      });
    },
    /** 導出按钮操作 */
    handleExport() {
      this.download(
        "biz/reservation/export",
        {
          ...this.queryParams,
        },
        `reservation_${new Date().getTime()}.xlsx`
      );
    },
    GoToQuote(item) {
      console.log(item);
      let query_params = {
        id: item.id,
        // customerId:item.customerId,
        // customerCode:item.customerCode,
        // customerName:item.customerName,
      };
      this.$router.push({
        path: "/quote/quote/add",
        query: query_params,
      });
    },
    //
    completeBtn(item) {
      this.open_receiverName = true;
      this.form_complete.receiverName = "";
      this.form_complete.completeTime = "";
      this.form_complete.id = item.id;
    },
    submitForm_complete() {
      this.$refs["form_complete"].validate((valid) => {
        if (valid) {
          if (this.form_complete.id != null) {
            putReservationComplete(this.form_complete).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open_receiverName = false;
              this.form_complete.receiverName = "";
              this.form_complete.completeTime = "";
              this.form_complete.id = "";
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style  >
.signType thead .el-table-column--selection .cell {
  display: none;
}
.dialog-footer {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
  
